/*
TO DO:

*/

#include <iostream>
#include <sstream>
#include <string>
#include <cstdlib>
#include "Book.h"

using namespace std;


Book::Book(string Author_init, string Title_init, string Publisher_init, int Year_init, int Edition_init, string ISBN_init) :  Author(Author_init), Title(Title_init), Publisher(Publisher_init), Year(Year_init), Edition(Edition_init), ISBN(ISBN_init)
{
    BookID = GenerateID();
    Loanable = true;
    LoaningDate = 0;
    Loaner = NULL;
}


Book::Book(string BookID_init, string Author_init, string Title_init, string Publisher_init, int Year_init, int Edition_init, string ISBN_init) :  BookID(BookID_init), Author(Author_init), Title(Title_init), Publisher(Publisher_init), Year(Year_init), Edition(Edition_init), ISBN(ISBN_init)
{
    Loanable = true;
    LoaningDate = 0;
    Loaner = NULL;
}


string Book::GenerateID() // BookID generator
{
    string Aut = (this->Author).substr(0,3);
    string Tit = (this->Title).substr(0,3);
    stringstream ss;
    ss << (this->Year);
    string Yea = ss.str();
    string result = Aut+Tit+Yea;
    return result;
}


void Book::ListBook() // konyv adatait kilistazo
{
    cout << "BookID: " << this->GetBookID() << endl;
    cout << "Author: " << this->GetAuthor() << endl;
    cout << "Title: " << this->GetTitle() << endl;
    cout << "Publisher: " << this->GetPublisher() << endl;
    cout << "Year: " << this->GetYear() << endl;
    cout << "Edition: " << this->GetEdition() << endl;
    cout << "ISBN: " << this->GetISBN() << endl;
    if(Loaner!=0)
    {
        //cout << Loaner->GetName() << " :\t" << Loaner->GetDeadline() << endl;
        cout << "Loaner name: " << Loaner->GetName() << endl;
    }
}


bool Book::SearchBook(int mitmit, std::string& szovegszoveg) // kereses
{
    bool found = false;
    switch(mitmit)
    {
        case 1:
            found = ToLower( Author.substr( 0,szovegszoveg.size() ) ) == ( ToLower(szovegszoveg) );
            break;
        case 2:
            found = ToLower( Title.substr( 0,szovegszoveg.size() ) ) == ( ToLower(szovegszoveg) );
            break;
        case 3:
            found = ToLower( ISBN.substr( 0,szovegszoveg.size() ) ) == ( ToLower(szovegszoveg) );
            break;
        case 4:
            found = ToLower( BookID.substr( 0,szovegszoveg.size() ) ) == ( ToLower(szovegszoveg) );
            break;
    }
    return found;
}


bool Book::Loan(LibraryMember* Loaner_param) // kolcsonzes (visszater a sikeresseggel)
{
    if(this->Loanable)
    {
        Loanable = false;
        Loaner = Loaner_param;
        return true;
    }
    return false;
}


ostream& operator<<(ostream &os,const Book *b) // operator<<
{
    os << b->GetBookID() << ';' <<  b->GetAuthor()<< ';' <<  b->GetTitle() << ';' <<  b->GetPublisher() << ';' <<  b->GetYear() << ';' <<  b->GetEdition() << ';' <<  b->GetISBN() << endl;
    return os;
}


string Book::ToLower(string mitmit) // kisbetuve konvertalo
{
    for(unsigned int i=0; i<mitmit.size(); ++i)
    {
        mitmit[i] = std::tolower(mitmit[i]);
    }
    return mitmit;
}

